# Create your views here.
from shipwreck.main.models import *
from shipwreck.main.helpers import *
from django.http import HttpResponse
from django.shortcuts import render_to_response
from shipwreck.main import data
import logging

def setupMap(request):
    for i in range(len(data.zones)):
        cz = data.zones[i]
        z = Zone.all().filter("number =", cz[0]).get()
        if z == None:
            logging.info("Creating zone %d: '%s' (%d,%d) to (%d,%d)" % (cz[0], cz[3], cz[1], cz[2], cz[4], cz[5]))
            z = Zone(number=cz[0], x=cz[1], y=cz[2], name=cz[3], width=cz[4], height=cz[5])
        logging.info("Setting zone %d: '%s' (%d,%d) to (%d,%d)" % (z.number, z.name, z.x, z.y, z.width, z.height))
        z.map=data.mapData[i]
        z.put()
    return HttpResponse("Setup zone information<br>Go <a href=\"/admin/\">Back</a>")
    
def viewZone(request, zone_id):
    logging.info("Searching for zone %d" % (int(zone_id)))
    zone = Zone.all().filter("number =", int(zone_id)).get()
    output = "\n<html><body>\n"
    if zone != None:
        output += "Zone %d : %s<br>" % (zone.number, zone.name)
        output += "From (%d,%d)" % (zone.x, zone.y)
        output += " to (%d,%d)<br>" % (zone.width, zone.height)
        output += zone.toHtml()
    else:
        output += "No zone of that id"
    output += "<br>Go <a href=\"/admin/\">Back</a></body></html>"
    return HttpResponse(output)

         
def setupItems(request):
    count = 1
    for i in data.item_prototypes:
        logging.info("Creating item %s", i)
        item = ItemPrototype(
                            number=count,
                            name = i['name'] or None,
                            type = i['type'] or None,
                            value = i['value'] or None,
                            primary_skill = i['primary_skill'] or None,
                            secondary_skill = i['secondary_skill'] or None,
                            uses = i['uses'] or None,
                            primary_bonus = i['primary_bonus'] or None,
                            secondary_bonus = i['secondary_bonus'] or None
                        )
        count += 1
    return HttpResponse("Setup item information for %d items<br>Go <a href=\"/admin/\">Back</a>" % (count))

def viewItem(request, item_id):
    i = ItemPrototype.all().filter("number =", int(item_id)).get()
    output = "\n<html><body>\n"
    if i != None:
        output += "Item %d : %s<br>" % (i.number, i.name)
        output += "Type %s, value %d<br>" % (data.itemlist[i.type], i.value)
        if (i.uses):
            output += "uses %d<br>" % (i.uses)
        if i.primary_skill:
            output += "Primary Skill: %s<br>" % (data.skilllist[i.primary_skill])
        if i.primary_bonus:
            output += "Primary Bonus: %d<br>" % (i.primary_bonus)
        if i.secondary_skill:
            output += "Secondary Skill: %s<br>" % (data.skilllist[i.secondary_skill])
        if i.secondary_bonus:
            output += "Secondary Bonus: %d<br>" % (i.secondary_bonus)
    else:
        output += "No item of that id"
    output += "<br>Go <a href=\"/admin/\">Back</a></body></html>"
    return HttpResponse(output)

def setupCreatures(request):
    count = 1
    for i in data.creature_prototypes:
        logging.info("Creating creature %s", i)
        creature = CreaturePrototype(
            number=count,
            name = i['name'],
            challenge_level = i['challenge_level'],
            zones = i['zones'],
            terrain = i['terrain'],
            # Attributes
            strength = i['strength'],
            agility = i['agility'],
            endurance = i['endurance'],
            perception = i['perception'],
            intelligence = i['intelligence'],
            social = i['social'],
            # Skills
            #combat
            skill_clubs = i['clubs'],
            skill_blades = i['blades'],
            skill_bows = i['bows'],
            skill_dodging = i['dodging'],
            # Other Attributes
            health = i['health'],
            #armour = i['armour'],
            #weapon = i['weapon']
        )
        creature.put()
        count += 1
    return HttpResponse("Setup creature information for %d creatures<br>Go <a href=\"/admin/\">Back</a>" % (count))

def viewCreature(request, creature_id):
    i = CreaturePrototype.all().filter("number =", int(creature_id)).get()
    if i:
        output = "\n<html><body>\n"
        output += "Creature %d: %s<br>" % (i.number, i.name)
        output += "Found in zones %s<br>" % (i.zones)
        output += "Found in Terrain of type %s<br>" % (i.terrain)
    else:
        output += "No item of that id"
    output += "<br>Go <a href=\"/admin/\">Back</a></body></html>"
    return HttpResponse(output)
def index(request):
    return render_to_response('admin_index.html', {'creatures': CreaturePrototype.all().fetch(1000), 'zones': Zone.all().fetch(1000)})
